<template>
	<div>
		<!-- 面包屑导航区 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
		  <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
		  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
		  <el-breadcrumb-item>商品列表</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 卡片视图区域 -->
		<el-card >
			<!-- 搜索与添加区域 -->
			<el-row :gutter="20">
				<el-col :span="8">
					<el-input placeholder="请输入内容" v-model="queryIofo.query" clearable @clear="getGoodsList()">
						<el-button slot="append" icon="el-icon-search" @click="getGoodsList()"></el-button>
					</el-input>
				</el-col>
				<el-col :span="4">
					 <el-button type="primary" @click="goAddPage">添加商品</el-button>
				</el-col>
			</el-row>
			<!-- 表格区域 -->
			<el-table :data="goodsList" border stripe>
				  <!-- 在这里绑定表格的数据goodsList -->
				  <el-table-column type="index"> </el-table-column>
				  <!-- 添加索引列 -->
			      <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
				  <!-- prop是取得userList中每一个对象中的对应属性值 -->
			      <el-table-column prop="goods_price" label="商品价格" width="95px"> </el-table-column>
				  <el-table-column prop="goods_weight" label="商品重量" width="95px"> </el-table-column>
				  <el-table-column  label="创建时间" width="180px">
					  <template slot-scope="scope">
						  {{scope.row.add_time | dataFormat}}
					  </template>
				  </el-table-column>
				  <el-table-column label="操作" width="130px" >
					  <template slot-scope="scope">
					  	<!-- 修改按钮 -->
					  	<el-button 
							type="primary" 
							icon="el-icon-edit" 
							size="mini" 
							@click="editDialog(scope.row)">
						</el-button>
					  	<!-- 删除按钮 -->
					  	<el-button 
							type="danger" 
							icon="el-icon-delete" 
							size="mini" 
							@click="removeGoodsById(scope.row.goods_id)">
						</el-button>
					  </template>
				  </el-table-column>
			</el-table>
			
			<!-- 分页区域 -->
			  <el-pagination
			      @size-change="handleSizeChange"
				   @current-change="handleCurrentChange"
				  :current-page="queryIofo.pagenum"
				  :page-sizes="[10, 20, 30, 40]"
				  :page-size="queryIofo.pagesize"
				  layout="total, sizes, prev, pager, next, jumper"
				  :total="total"
				  background>
				</el-pagination>
		</el-card>
		
		<!-- 修改用户对话框 -->
		<el-dialog
		  title="修改商品信息"
		  :visible.sync="editdialogVisible"
		  width="50%"
		 >
		 <!-- 内容主体区域，这里有一个匿名插槽 -->
		<el-form :model="editForm" :rules="addFormRules" ref="editFormRef" label-width="100px">
				<el-form-item label="商品名称" prop="goods_name">
					<el-input v-model="editForm.goods_name" ></el-input>
				</el-form-item>
				<el-form-item label="商品价格" prop="goods_price">
				  <el-input v-model="editForm.goods_price"></el-input>
				</el-form-item>
				<el-form-item label="商品重量" prop="goods_weight">
				  <el-input v-model="editForm.goods_weight"></el-input>
				</el-form-item>
		</el-form>
		 
		  <!-- 底部区域，这里是一个具名插槽 -->
		  <span slot="footer" class="dialog-footer">
		    <el-button @click="editdialogVisible = false">取 消</el-button>
		    <el-button type="primary" @click="editGoodsInfo">修改</el-button>
		  </span>
		</el-dialog>
	</div>
</template>

<script>
	export default{
		data(){
			return {
				queryIofo:{
					query:'',
					pagenum:1,
					pagesize:10
				},
				//商品列表
				goodsList:[],
				//总商品个数
				total:0,
				editdialogVisible:false,
				editForm:{},
				addFormRules:{
					goods_name:[{ required: true, message: '请输入邮箱', trigger: 'blur' }],
					goods_price:[{ required: true, message: '请输入邮箱', trigger: 'blur' }],
					goods_weight:[{ required: true, message: '请输入邮箱', trigger: 'blur' }],
				}
			}
		},
		created(){
			this.getGoodsList()
		},
		methods:{
			async getGoodsList(){
				const{data:res}=await this.$http.get('goods',{
					params:this.queryIofo
				})
			if(res.meta.status!==200)return this.$message.error(res.meta.msg)
			this.goodsList=res.data.goods
			this.total=res.data.total
			},
			//监听每页显示的条数
			handleSizeChange(newSize){
				//一旦每页几条发生变化，则触发这个事件，把数据中的pagesize更新
				this.queryIofo.pagesize=newSize
				//重新用新的queryIofo参数获取用户数据，渲染页面
				this.getGoodsList()
			},
			//监听页码变化
			handleCurrentChange(newPage){
				this.queryIofo.pagenum=newPage
				//重新用新的queryIofo参数获取用户数据，渲染页面
				this.getGoodsList()
			},
			//删除数据
			async removeGoodsById(id){
				//弹窗询问是否删除数据
				const confirmResult=await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
				          confirmButtonText: '确定',
				          cancelButtonText: '取消',
				          type: 'warning'
				        }).catch(err=>err)
				//如果用户确认删除，则返回字符串confirm
				//如果用户确认取消，则返回字符串cancel
				if(confirmResult!=='confirm')return this.$message.info("已经取消了删除")
				const{data:res}=await this.$http.delete('goods/'+id)
				if(res.meta.status!==200)return this.$message.error(res.meta.msg)
				this.getGoodsList()
				this.$message.success(res.meta.msg)
			},
			// 添加商品的页面是一个新的页面,不再以弹窗的形式
			//所以需要用到vue-router的编程式导航功能,以此来完成组件间的跳转
			goAddPage(){
				this.$router.push('/goods/add')
			},
			//点击编辑按钮，弹出修改对话框
			async editDialog(row){
				//发起请求，根据id,获取参数，存储到editForm中
				const{data:res}=await this.$http.get('goods/'+row.goods_id)
				if(res.meta.status!==200)return this.$message.error(res.meta.msg)
				// this.$message.success(res.meta.msg)
				//存储到editForm中
				this.editForm=res.data
				this.editdialogVisible=true
			},
			async editGoodsInfo(){
				//修改完成后发起请求
				const{data:res}=await this.$http.put('goods/'+this.editForm.goods_id,this.editForm)
				if(res.meta.status!==200)return this.$message.error(res.meta.msg)
				this.$message.success("修改商品成功")
				this.editdialogVisible=false
			}
		}
	}
</script>

<style  lang="less" scoped>
</style>
